package com.uplift.mgt.building.controller;

import com.uplift.common.base.ServiceReslut;
import com.uplift.common.result.IResultable;
import com.uplift.common.result.ResultWrapper4Layui;
import com.uplift.mgt.building.model.RealtyInfo;
import com.uplift.mgt.building.model.RealtyRoomModel;
import com.uplift.mgt.building.service.IRealtyInfoService;
import com.uplift.mgt.building.service.IRealtyRoomService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 物业信息相关
 * 
 * @author bcwang
 *
 */
@Controller
@RequestMapping(value = "/realty")
public class RealtyController {

	@Autowired
	private IRealtyInfoService realtyInfoService;

	@Autowired
	private IRealtyRoomService realtyRoomService;

	/**
	 * 查询物业信息
	 *
	 * @param request
	 * @param realtyInfo
	 * @return IResultable
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public IResultable list(HttpServletRequest request, RealtyInfo realtyInfo) {
		try {
			List<Map<String, Object>> list = this.realtyInfoService.queryRealtyInfo(realtyInfo);
			return ResultWrapper4Layui.wrapListPage(list, ResultWrapper4Layui.SUCC, "查询成功");
		} catch (Exception e) {
			return ResultWrapper4Layui.wrapListPage(null, ResultWrapper4Layui.FAIL, "查询失败");
		}
	}

	/**
	 * 查询物业信息
	 *
	 * @param request
	 * @param realtyInfo
	 * @return IResultable
	 */
	@RequestMapping(value = "/all-list")
	@ResponseBody
	public IResultable allList(HttpServletRequest request, RealtyInfo realtyInfo) {
		try {
			List<Map<String, Object>> list = this.realtyInfoService.queryAllRealtyInfo(realtyInfo);
			return ResultWrapper4Layui.wrapListPage(list, ResultWrapper4Layui.SUCC, "查询成功");
		} catch (Exception e) {
			return ResultWrapper4Layui.wrapListPage(null, ResultWrapper4Layui.FAIL, "查询失败");
		}
	}

	/**
	 * 新增物业记录
	 *
	 * @param request
	 * @param realtyInfo
	 * @return IResultable
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public IResultable save(HttpServletRequest request, RealtyInfo realtyInfo) {
		try {
			ServiceReslut serviceReslut = this.realtyInfoService.addRealtyInfo(realtyInfo);
			if (serviceReslut.getCode() > 0) {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.SUCC, "保存成功");
			} else {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "保存失败," + serviceReslut.getMessage());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "保存失败");
		}
	}

	/**
	 * 删除物业记录
	 *
	 * @param request
	 * @param realtyInfo
	 * @return IResultable
	 */
	@RequestMapping(value = "/remove")
	@ResponseBody
	public IResultable remove(HttpServletRequest request, RealtyInfo realtyInfo) {
		try {
			ServiceReslut serviceReslut = this.realtyInfoService.deleteRealtyInfo(realtyInfo);
			if (serviceReslut.getCode() > 0) {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.SUCC, "删除成功");
			} else {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "删除失败," + serviceReslut.getMessage());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "删除失败");
		}
	}

	/**
	 * 删除房间记录
	 *
	 * @param request
	 * @param realtyRoomModel
	 * @return IResultable
	 */
	@RequestMapping(value = "/remove-room")
	@ResponseBody
	public IResultable removeRoom(HttpServletRequest request, RealtyRoomModel realtyRoomModel) {
		try {
			ServiceReslut serviceReslut = this.realtyRoomService.deleteRealtyRoom(realtyRoomModel);
			if (serviceReslut.getCode() > 0) {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.SUCC, "删除成功");
			} else {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "删除失败," + serviceReslut.getMessage());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "删除失败");
		}
	}

	/**
	 * 更新物业记录
	 *
	 * @param request
	 * @param realtyInfo
	 * @return IResultable
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public IResultable update( HttpServletRequest request,RealtyInfo realtyInfo) {
		try {
			ServiceReslut serviceReslut = this.realtyInfoService.updateRealtyInfo(realtyInfo);
			if (serviceReslut.getCode() > 0) {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.SUCC, "更新成功");
			} else {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "更新失败," + serviceReslut.getMessage());
			}
		} catch (Exception e) {
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "更新失败");
		}
	}

	/**
	 * 更新房间号记录
	 *
	 * @param request
	 * @param realtyRoomModel
	 * @return IResultable
	 */
	@RequestMapping(value = "/update-room")
	@ResponseBody
	public IResultable updateRoom(HttpServletRequest request, RealtyRoomModel realtyRoomModel) {
		try {
			ServiceReslut serviceReslut = this.realtyRoomService.updateRealtyRoom(realtyRoomModel);
			if (serviceReslut.getCode() > 0) {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.SUCC, "更新成功");
			} else {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "更新失败," + serviceReslut.getMessage());
			}
		} catch (Exception e) {
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "更新失败");
		}
	}

	/**
	 * 更新收款记录
	 *
	 * @param request
	 * @param realtyInfo
	 * @return IResultable
	 */
	@RequestMapping(value = "/get-one")
	@ResponseBody
	public IResultable getOne(HttpServletRequest request, RealtyInfo realtyInfo, String type) {
		try {
			List<Map<String, Object>> list = this.realtyInfoService.queryRealtyInfo(realtyInfo);
			if (list!=null && list.size()>0) {

				Map<String, Object> data = list.get(0);

//				if(StringUtils.equals(data.get("typeCode").toString(), "BUILD")) {


					RealtyRoomModel realtyRoomModel = new RealtyRoomModel();
					if(StringUtils.equals(type, "2")) {
						realtyRoomModel.setIsShow("2");
					}
					realtyRoomModel.setRealtyId(data.get("id").toString());
					list = this.realtyRoomService.queryRealtyRoom(realtyRoomModel);
//					if(list!=null && list.size()>0) {
//						Map<String, Object> room = list.get(0);
//						data.put("leaseManageCost", room.get("manageCost"));
//					}
					data.put("rooms", list);
//				}

				return ResultWrapper4Layui.wrapMap(data, ResultWrapper4Layui.SUCC, "查询成功");
			} else {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.SUCC, "查询成功");
			}
		} catch (Exception e) {
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "查询失败");
		}
	}

	/**
	 * 导入楼宇
	 * @return
	 */
	@RequestMapping(value = "/import-realty")
	@ResponseBody
	public IResultable importRealty(MultipartHttpServletRequest request) {
		try {
			this.realtyInfoService.importRealty(request);
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.SUCC, "导入成功");
		}catch (Exception e) {
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, e.getMessage());
		}
	}

	/**
	 * 导入楼宇图片
	 * @return
	 */
	@RequestMapping(value = "/import-realty-pic")
	@ResponseBody
	public IResultable importRealtyPic(MultipartHttpServletRequest request) {
		try {
			this.realtyInfoService.importRealtyPic(request);
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.SUCC, "导入成功");
		}catch (Exception e) {
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, e.getMessage());
		}
	}
}
